<?php
namespace app\bancard\controller;
use think\facade\Cache;
use app\common\validate\CountdownValidate;
use app\bancard\model\SetupCountdown as CountdownModel;
class Countdown extends Common {
	
	//首页
	public function index(){
		return $this->fetch();
	}

	public function lists(){
		$user_id = request()->param('user_id');
		$school_id = request()->param('school_id');
		$ntitle =request()->param('keywords');
		$title = isset($ntitle)?$ntitle:'';
		$limit = request()->param('limit');
		$offset = request()->param('offset');
		$start = ($offset-1)*$limit;
		$map =[
			'user_id'=>$user_id,
			'school_id'=>$school_id
		];
		if($title){
			$count = db('setup_countdown')->where($map)->whereLike('title',"%$title%")->count();
			$result = db('setup_countdown')->where($map)->whereLike('title',"%$title%")->limit($start,$limit)->order('sort desc,id desc')->select();
		}else{
			$count = db('setup_countdown')->where($map)->count();
			$result = db('setup_countdown')->where($map)->limit($start,$limit)->order('sort desc,id desc')->select();
		}
		return showTable(0, 'OK', $count ,$result, 200);
	}
	//添加
	public function add(){
		
		if(request()->isPost()){
			$data = request()->param();
			$data['created_at'] = date('Y-m-d H:i:s', time());
			$data['user_id'] = session('admin.id');
			$data['school_id'] = session('schoolInfo.id');
			$validate = new CountdownValidate;
			if (!$validate->scene('add')->check($data)) {
				return show(0, $validate->getError(), 0 , 200);
			}else{
				Cache::clear('setup_countdown');
				$rest = (new CountdownModel())->strict(false)->insert($data);
				return show(1, '添加成功', $rest, 200);
			}			
		}
		$egetClass = $this->getClass();
		$this->assign('egetClass', json_encode($egetClass['data']));
		return $this->fetch();
	}
	
	//编辑
	public function edit(){
		
		if(request()->isPost()){
			$data = request()->param();
			$validate = new CountdownValidate;
			if (!$validate->scene('edit')->check($data)) {
				return show(0, $validate->getError(), 0 , 200);
			}else{
				Cache::clear('setup_countdown');
				$rest = (new CountdownModel())->strict(false)->where('id',$data['id'])->update($data);
				return show(1, '修改成功', $rest, 200);
			}			
		}
		$data = request()->param();
		$result = db('setup_countdown')->where('id','=',$data['id'])->find();
		$this->assign('result',$result);
		return $this->fetch();
	}
	
	
	public function seting(){
		return $this->fetch();
	}
	
	//批量删除
	public function delete(){
		Cache::clear('setup_countdown');
		$data = request()->param();
		$result = db('setup_countdown')->where(['user_id'=>$data['user_id']])->delete($data['ids']);
		return show(1, '删除成功', $result, 200);
	}
	
	//排序
	public function sort(){
		$data = request()->param();
		
		$validate = new CountdownValidate;
		
		if (!$validate->scene('sort')->check($data)) {
			return show(config('code.error'), 'error', $validate->getError(), 200);
		}else{
			Cache::clear('setup_countdown');
			$rest = (new CountdownModel())->save(['sort'=>$data['sort']],['id'=>$data['id']]);
			return show(config('code.success'), 'OK', $rest, 200);
		}
			
	}
}